package com.yupi.yuojcodesandbox.Utils;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.StrUtil;
import com.yupi.yuojcodesandbox.model.ExecuteMessage;

import java.io.*;

public class ProcessUtils {

    public static ExecuteMessage runProcessAndGetMessage(Process process , String opName){
        ExecuteMessage executeMessage = new ExecuteMessage();

        try {

            StopWatch stopWatch = new StopWatch();
            stopWatch.start();

            Integer exitValue = process.waitFor();
            executeMessage.setExitValue(exitValue);

            if(exitValue == 0){
                System.out.println(opName + " 成功 !");

                //分批获取进程的信息 是正常是输出流
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String compileOutputLine;

                StringBuilder compileOutputStringBuilder = new StringBuilder();

                //按行读取编译结果
                while((compileOutputLine = bufferedReader.readLine()) != null){
                    compileOutputStringBuilder.append(compileOutputLine).append("\n");
                }
                executeMessage.setMessage(compileOutputStringBuilder.toString());

            }else{
                System.out.println(opName + " failed ! error code : " + exitValue);

                //分批获取进程的信息 是编译错误的输出流
                BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                String errorCompileOutputLine;
                StringBuilder errorCompileOutputStringBuilder = new StringBuilder();
                //按行读取编译结果
                while((errorCompileOutputLine = errorBufferedReader.readLine()) != null){
                    errorCompileOutputStringBuilder.append(errorCompileOutputLine).append("\n");
                }
                executeMessage.setErrorMessage(errorCompileOutputStringBuilder.toString());

            }

            stopWatch.stop();
            executeMessage.setTime(stopWatch.getLastTaskTimeMillis());
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }


        System.out.println(executeMessage);

        return executeMessage;
    }

    /**
     *
     * @param process
     * @param opName
     * @return
     */
    public static ExecuteMessage runInteractProcessAndGetMessage(Process process , String opName , String args) throws IOException {
        ExecuteMessage executeMessage = new ExecuteMessage();



        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        OutputStream outputStream = process.getOutputStream();

        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        String s[] = args.split(" ");
        String join = StrUtil.join("\n" , s) + "\n";
        outputStreamWriter.write(join);
        outputStreamWriter.flush();

        InputStream inputStream = process.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder OutputStringBuilder = new StringBuilder();

        String OutputLine;
        while((OutputLine = bufferedReader.readLine()) != null) {
            OutputStringBuilder.append(OutputLine).append("\n");
            executeMessage.setMessage(OutputStringBuilder.toString());
        }

        outputStreamWriter.close();
        outputStream.close();
        inputStream.close();
        process.destroy();

        return executeMessage;
    }


}
